/*
 * Class name: CGamePlay
 * Description: 
 * Author: HuyNA
 * Date: 2014/04/20
 */

#include "GamePlay.h"
#include "Constant.h"

USING_NS_CC;

CGamePlay::CGamePlay()
{
	pFish = new CFish();
	curAction = NULL;
}

CGamePlay::~CGamePlay()
{

}

void CGamePlay::actionTap()
{
	/* Get current position */
	cocos2d::CCPoint posUp = this->pFish->getPosition();
	/* Calculate position up */
	posUp.y += JUMP_HEIGHT;
	//
	cocos2d::CCMoveTo* moveUp = CCMoveTo::create(0.5, posUp);
	cocos2d::CCSpawn* fly = CCSpawn::create(NULL, CCEaseSineOut::create(moveUp), NULL);
	/* Calculate position down */
	cocos2d::CCPoint posDown = ccp(posUp.x, 95);
	float duraDown = getDurationDown(posUp, posDown);
	//
	cocos2d::CCMoveTo* moveDown = CCMoveTo::create(duraDown, posUp);
	cocos2d::CCSpawn* down = CCSpawn::create(NULL, CCEaseSineOut::create(moveDown), NULL);
	//
	cocos2d::CCSequence* tap = CCSequence::create(fly, down, NULL);
	//
	this->curAction = tap;
	//
	this->pFish->runAction(tap);
}

float CGamePlay::getDurationDown(CCPoint up, CCPoint down)
{
    //cho toc do xuong nhanh hon len gap doi...
    float dy = up.y - down.y;
    float duraDown;

    //neu dy <= kjumheight => lay thoi gian khong doi => neu khong thi se nhanh qua
    if (dy <= JUMP_HEIGHT)
    {
    	duraDown = 0.5 * 1/2;
    }
    else //neu cao hon thi tinh theo ti le thuan
    {
        duraDown = dy * (0.5 * 1/2) / JUMP_HEIGHT;
    }

    return duraDown;
}

void CGamePlay::update(float delta)
{
	this->pFish = (CFish*)getOwner();
	if(pFish->gStatus == isIdle)
	{
		return;
	}
	else if(pFish->gStatus == isTap)
	{
		pFish->gStatus = isIdle;
		actionTap();
	}
}

void CGamePlay::stopAction()
{
	if(this->curAction != NULL)
	{
		/* Stop current action */
		pFish->stopAction(curAction);
		this->curAction = NULL;
	}
}
